import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getUserInfo } from '@/api/index'

export const useUser = defineStore('user', () => {
  
  const token = ref(localStorage.getItem('token'))

  const show_head = ref(true)

  const current_nav = ref(0)

  const nav_name_list = ref([])

  const captchaId = ref('')

  const is_login = ref(!!localStorage.getItem('token'))

  const user_info = ref(localStorage.getItem('user_info') ? JSON.parse(localStorage.getItem('user_info')) : {})

  function logOut () {
    is_login.value = false
    localStorage.setItem('user_info','')
    localStorage.setItem('token','')
  }

  async function getUserInfoApi () {
    let res = await getUserInfo()
    localStorage.setItem('user_info',JSON.stringify(res.data))
    user_info.value = res.data
  }

  function logIn (data) {
    is_login.value = true
    user_info.value = data
  }

  return {
    is_login,
    user_info,
    token,
    captchaId,
    current_nav,
    nav_name_list,
    logOut,
    logIn,
    getUserInfoApi,
    show_head
  }
})
